package com.powernode.driver.service.impl;


import com.alibaba.nacos.common.codec.Base64;
import com.powernode.driver.properties.TencentProperties;
import com.powernode.driver.service.CosService;
import com.powernode.driver.service.OcrService;
import com.powernode.model.vo.driver.CosUploadVo;
import com.powernode.model.vo.driver.IdCardOcrVo;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class OcrServiceImpl implements OcrService {

    @Resource
    private TencentProperties tencentProperties;

    @Resource
    private CosService cosService;

    /**
     * 1.识别身份证文字信息
     * 2.存储图片到cos
     */
    @Override
    public IdCardOcrVo idCardOcr(MultipartFile file) {
        try {
            // 将图片转成base64  将2进制数据转成文本数据  便于传输
            byte[] bytes = Base64.encodeBase64(file.getBytes());

            //文件文本数据
            String idCardBase64 = new String(bytes);

            //创建凭证
            Credential credential = new Credential(tencentProperties.getSecretId(), tencentProperties.getSecretKey());

            //获取ocr client
            OcrClient ocrClient = new OcrClient(credential, tencentProperties.getRegion());

            //构建请求对象
            IDCardOCRRequest request = new IDCardOCRRequest();
            //设置文件数据
            request.setImageBase64(idCardBase64);

            //发送请求获取响应
            IDCardOCRResponse response = ocrClient.IDCardOCR(request);

            IdCardOcrVo idCardOcrVo = new IdCardOcrVo();

            //判断当前是正面还是反面
            if (StringUtils.hasText(response.getName())) {
                //正面
                idCardOcrVo.setName(response.getName());
                Date date = DateTimeFormat.forPattern("yyyy/MM/dd").parseDateTime(response.getBirth().toString()).toDate();
                idCardOcrVo.setBirthday(date);
                idCardOcrVo.setGender("男".equals(response.getSex()) ? "1" : "2");
                idCardOcrVo.setIdcardAddress(response.getAddress());
                idCardOcrVo.setIdcardNo(response.getIdNum());

                //将身份证图片存储到cos中
                CosUploadVo cosUploadVo = cosService.upload(file, "idCard");

                idCardOcrVo.setIdcardFrontUrl(cosUploadVo.getUrl());
                idCardOcrVo.setIdcardFrontShowUrl(cosUploadVo.getShowUrl());

            } else {
                //反面
                String idcardExpire = response.getValidDate().split("-")[1];

                Date date = DateTimeFormat.forPattern("yyyy.MM.dd").parseDateTime(idcardExpire).toDate();
                //设置过期时间
                idCardOcrVo.setIdcardExpire(date);

                //存储到cos
                CosUploadVo cosUploadVo = cosService.upload(file, "idCard");
                idCardOcrVo.setIdcardBackUrl(cosUploadVo.getUrl());
                idCardOcrVo.setIdcardBackShowUrl(cosUploadVo.getShowUrl());
            }


            return idCardOcrVo;
        } catch (IOException | TencentCloudSDKException e) {
            throw new RuntimeException(e);
        }
    }
}
